﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GdHotel.Data;
using GdHotel.Data.Interfaces;
using UniqueTechnologies.Attributes;
using UniqueTechnologies.Data.Interfaces;

namespace GdHotel.Web.Controllers
{
    [LanguageSupport]
    [Authorize(Roles = "Admin")]
    public class ManageMainMenuItemsController : BaseController
    {
        public IMainMenuItemsRepository MainMenuItemsRepository { get; set; }
        public override ILanguagesRepository LanguagesRepository { get; set; }

        public ManageMainMenuItemsController(IMainMenuItemsRepository mainMenuItemsRepository, ILanguagesRepository languagesRepository)
        {
            MainMenuItemsRepository = mainMenuItemsRepository;
            LanguagesRepository = languagesRepository;
        }

        //
        // GET: /ManageMainMenuItems/

        public ViewResult Index()
        {
            var mainMenuItems = MainMenuItemsRepository
                .FindAllByLang(Language)
                .OrderBy(menuItem => menuItem.Order);
            return View(mainMenuItems);
        }

        //
        // GET: /ManageMainMenuItems/

        public ViewResult All()
        {
            return View();
        }

        //
        // GET: /ManageMainMenuItems/Details/5

        public ViewResult Details(Int32 id)
        {
            var menuItem = MainMenuItemsRepository.GetById(id);
            return View(menuItem);
        }

        //
        // GET: /ManageMainMenuItems/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /ManageMainMenuItems/Create

        [HttpPost]
        public ActionResult Create(MainMenuItem menuItem)
        {
            if (ModelState.IsValid)
            {
                menuItem.LanguageId = Language.Id;
                MainMenuItemsRepository.Insert(menuItem);
                MainMenuItemsRepository.Save();
                return RedirectToAction("Index");
            }

            return View(menuItem);
        }

        //
        // GET: /ManageMainMenuItems/Edit/5

        public ActionResult Edit(int id)
        {
            var menuItem = MainMenuItemsRepository.GetById(id);
            return View(menuItem);
        }

        //
        // POST: /ManageMainMenuItems/Edit/5

        [HttpPost]
        public ActionResult Edit(MainMenuItem menuItem)
        {
            var dbMainMenuItem = MainMenuItemsRepository.GetById(menuItem.Id);
            if (ModelState.IsValid)
            {
                UpdateModel(dbMainMenuItem);
                MainMenuItemsRepository.Save();
                return RedirectToAction("Index");
            }
            return View(menuItem);
        }

        //
        // GET: /ManageMainMenuItems/Delete/5

        public ActionResult Delete(int id)
        {
            var menuItem = MainMenuItemsRepository.GetById(id);
            return View(menuItem);
        }

        //
        // POST: /ManageMainMenuItems/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var menuItem = MainMenuItemsRepository.GetById(id);
            MainMenuItemsRepository.Delete(menuItem);
            MainMenuItemsRepository.Save();
            return RedirectToAction("Index");
        }

        //
        // GET: /MenuItemsRepository/IncrOrder/5

        public ActionResult IncrOrder(Int32 id)
        {
            var menuItem = MainMenuItemsRepository.GetById(id);
            if (menuItem == null)
                return HttpNotFound();

            if (menuItem.Order <= 0)
                return RedirectToAction("Index");

            menuItem.Order--;
            var neighbours = MainMenuItemsRepository
                .FindAllByLang(Language)
                .Where(item => item.Order == menuItem.Order)
                .Where(item => item.Id != menuItem.Id);
            foreach (var neighbour in neighbours)
                neighbour.Order++;

            MainMenuItemsRepository.Save();
            return RedirectToAction("Index");
        }

        //
        // GET: /MenuItemsRepository/DecrOrder/5
    
        public ActionResult DecrOrder(Int32 id)
        {
            var menuItem = MainMenuItemsRepository.GetById(id);
            if (menuItem == null)
                return HttpNotFound();

            menuItem.Order++;
            var neighbours = MainMenuItemsRepository
                .FindAllByLang(Language)
                .Where(item => item.Order == menuItem.Order)
                .Where(item => item.Id != menuItem.Id);
            foreach (var neighbour in neighbours)
                neighbour.Order--;

            MainMenuItemsRepository.Save();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            MainMenuItemsRepository.Dispose();
            base.Dispose(disposing);
        }
    }
}